#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/input.h>
#include <linux/ide.h>
#include <asm/mach/map.h>
#include <linux/timer.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/atomic.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/bitops.h>
#include <linux/semaphore.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define KEYINPUT_CNT         1
#define KEYINPUT_NAME        "keyinput"

#define KEY_NUM            1
#define KEY0VALUE          0X01
#define INVAKEY            0XFF

struct irq_keydesc{
    int gpio;
    int irqnum;
    unsigned char value;
    char name[10];
    irqreturn_t (*handler)(int, void *);
};

struct keyinput_dev{
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    int major;
    int minor;
    struct device_node  *nd;
    struct irq_keydesc irqkeydesc;
    struct timer_list timer;
    atomic_t keyvalue;
    atomic_t releasekey;
    unsigned char curkeynum;
    struct input_dev *inputdev;
};

struct keyinput_dev keyinputdev;

static irqreturn_t key0_handle(int irq,void *dev_id)
{
    struct keyinput_dev *dev = (struct keyinput_dev *)dev_id;

    dev->curkeynum = 0;
    dev->timer.data = (volatile long)dev_id;
    mod_timer(&dev->timer,jiffies+msecs_to_jiffies(5));
    return IRQ_RETVAL(IRQ_HANDLED);
}

void timer_function(unsigned long arg)
{
    unsigned char value;
    unsigned char num;
    struct irq_keydesc *keydesc;
    struct keyinput_dev *dev = (struct keyinput_dev*)arg;

    num = dev->curkeynum;
    keydesc = &dev->irqkeydesc;
    
    value = gpio_get_value(keydesc->gpio);
    if(value == 0){
        input_report_key(dev->inputdev,keydesc->value,1);
        input_sync(dev->inputdev);
    }
    else{
        input_report_key(dev->inputdev,keydesc->value,0);
        input_sync(dev->inputdev);
    }
}


static int keyio_init(void)
{
    int ret;

    keyinputdev.nd = of_find_node_by_path("/key");
    if(keyinputdev.nd == NULL){
        printk("gpio node nost find!\r\n");
        return -EINVAL;
    }
    keyinputdev.irqkeydesc.gpio = of_get_named_gpio(keyinputdev.nd,"key-gpio",0);
    if(keyinputdev.irqkeydesc.gpio < 0){
        printk("can't get key-gpio\r\n");
        return -EINVAL;
    }

    gpio_request(keyinputdev.irqkeydesc.gpio,"KEY0");
    gpio_direction_input(keyinputdev.irqkeydesc.gpio);

    /*申请中断*/
    memset(keyinputdev.irqkeydesc.name,0,sizeof(keyinputdev.irqkeydesc.name));
    sprintf(keyinputdev.irqkeydesc.name,"KEY0");
    
    keyinputdev.irqkeydesc.irqnum = irq_of_parse_and_map(keyinputdev.nd,0);
    keyinputdev.irqkeydesc.handler = key0_handle;
    keyinputdev.irqkeydesc.value =  KEY_0;
    ret = request_irq(keyinputdev.irqkeydesc.irqnum,keyinputdev.irqkeydesc.handler,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING,keyinputdev.irqkeydesc.name,&keyinputdev);
    if(ret < 0){
		printk("irq %d request failed!\r\n", keyinputdev.irqkeydesc.irqnum);
		return -EFAULT;
	}
    init_timer(&keyinputdev.timer);
    keyinputdev.timer.function = timer_function;

    keyinputdev.inputdev = input_allocate_device();
    keyinputdev.inputdev->name = KEYINPUT_NAME;
    keyinputdev.inputdev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
    input_set_capability(keyinputdev.inputdev, EV_KEY, KEY_0);
    ret = input_register_device(keyinputdev.inputdev);
    if(ret){
        printk("register input device failed!\r\n");
        return ret;
    }
    return 0;
}


static int __init keyinput_init(void)
{
    keyio_init();
    return 0;
}

static void __exit keyinput_exit(void)
{
    del_timer_sync(&keyinputdev.timer);	/* 删除定时器 */
    input_unregister_device(keyinputdev.inputdev);
    input_free_device(keyinputdev.inputdev);

    gpio_free(keyinputdev.irqkeydesc.gpio);
    free_irq(keyinputdev.irqkeydesc.irqnum, &keyinputdev);
}


module_init(keyinput_init);
module_exit(keyinput_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("GARY");